package com.xfyuao.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xfyuao.dto.EmpDto;
import com.xfyuao.entity.Dept;
import com.xfyuao.entity.Emp;
import com.xfyuao.service.EmpService;
import com.xfyuao.util.PageUtil;
import com.xfyuao.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xfyuao
 * @since 2020-05-29
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
	@Autowired
	private EmpService empService;

	/**
	 * http://localhost:8080/insert 新增，id自增
	 * 
	 * @param emp
	 * @return 0代表成功
	 */
	@RequestMapping("/insert")
	public Object insert(Emp emp) {
		System.out.println("接收到的要新增的emp:" + emp);
		boolean bl = empService.save(emp);
		if (bl) {
			return new Result(0, "添加成功！");
		}
		return new Result(2, "添加失败！");
	}

	/**
	 * http://localhost:8080/delete 根据id删除
	 * 
	 * @param empNo
	 * @return 0代表成功
	 */
	@RequestMapping("/delete")
	public Object delete(Integer empNo) {
		boolean bl = empService.removeById(empNo);
		if (bl) {
			return new Result(0, "删除成功！");
		}
		return new Result(1, "删除失败！");
	}

	/**
	 * http://localhost:8080/update 修改
	 * 
	 * @param emp
	 * @return 0代表成功
	 */
	@RequestMapping("/update")
	public Object update(Emp emp) {
		boolean bl = empService.updateById(emp);
		if (bl) {
			return new Result(0, "修改成功！");
		}
		return new Result(1, "修改失败！");
	}

	/**
	 * http://localhost:8080/selectPage 动态条件分页查
	 * 使用了mybatis plus的Page<T>对象接收分页条件，
	 * 前台页面laytable渲染时，请求参数修改为current和size
	 * @param empDto 查询条件
	 * @return layui数据表格要的数据格式
	 */
	@RequestMapping("/selectPage")
	public Object selectPage(EmpDto et, Page<Emp> page) {
		System.out.println("emp查询条件：" + et);
		
		// 出入条件和分页数据，调用分页方法查询
		//Page<Emp> p = empService.page(page, myWhere(empDto));
		IPage<Emp> p = empService.selectEmpPage(page, et);
		
		return new Result(0, "查询成功", p.getRecords(), p.getTotal());
	}
	
	/*private QueryWrapper<Emp> myWhere(EmpDto et){
		QueryWrapper<Emp> qw = new QueryWrapper<Emp>();
		if(et.getEname()!=null && !et.getEname().isEmpty()) {
			qw.like("ename", et.getEname());
		}
		if(et.getDeptNo()!=null && et.getDeptNo()!=0) {
			qw.eq("dept_no", et.getDeptNo());
		}
		if(et.getHiredate()!=null) {
			qw.ge("hiredate", et.getHiredate());
		}
		if(et.getHiredate()!=null) {
			qw.le("hiredate", et.getHiredate2());
		}
		return qw;
	}*/
}
